﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;


namespace PDS2_Launcher
{
    public partial class Form1 : Form
    {
        float version = 0.1f;

        PDS2.SaveGame save;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetParams();
            string ver = version.ToString();
            label1.Text = "Launcher: " + ver.Replace(",",".");

            if (Directory.Exists("Content/Profiles"))
                GetProfiles();
        }

        private void GetProfiles()
        {
            string[] files = Directory.GetFiles("Content/Profiles");
            foreach (string file in files)
            {
                comboBox1.Items.Add(Path.GetFileNameWithoutExtension(file));
            }
            comboBox1.SelectedIndex = 0;
        }

        private void SetParams()
        {
            if (radioButton1.Checked)
            {
                textBox1.Text = "-FULLSCREEN=true";
            }
            if (radioButton2.Checked)
            {
                textBox1.Text = "-FULLSCREEN=false";
            }
            textBox1.Text += "-WIDTH=" + textBox2.Text + " ";
            textBox1.Text += "-HEIGHT=" + textBox3.Text + " ";
            textBox1.Text += "-PROFILE=" + comboBox1.Text;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            SetParams();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            SetParams();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            SetParams();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            SetParams();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!textBox1.Text.Contains("-PROFILE="))
            {
                MessageBox.Show("Choose a profile");
                return;
            }
            if (textBox1.Text.Split(' ')[3].Length < 10)
            {
                MessageBox.Show("Choose a profile");
                return;
            }

            Process.Start("PDS2.exe", textBox1.Text);
            Application.Exit();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (NewUser newUser = new NewUser())
            {
                if (newUser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    AddNewUser(newUser.textBox1.Text);
                }
            }
        }

        void AddNewUser(string user)
        {
            if (!Directory.Exists("Content/Profiles"))
                Directory.CreateDirectory("Content/Profiles");
            comboBox1.Items.Add(user);
            if (comboBox1.Text == "")
                comboBox1.SelectedIndex = 0;
            //SetParams();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetParams();
        }
    }
}
